﻿using System;
using System.Collections.Generic;
using System.Text;
using Wicresoft.EKA.NRemedy;
using System.Threading;
using System.Windows.Forms;
using Wicresoft.EKA.NRemedy.CodeGenerate;

namespace Wicresoft.EKA.NRemedy.CodeGenerator.WinForm
{
    public class GetFormsThread : ARThread
    {

        public LoginDelegate GetFormsCompeleted;

        public void Start()
        {
            _thread = new Thread(GetListRegularForm);
            _thread.Start();
        }

        private void GetListRegularForm()
        {
            bool isSuccess = false;
            string exceptionMessage = string.Empty;
            try
            {
                ARSchema asrsch = new ARSchema(_context);
                if (GlobalCache.FormCache == null)
                {
                    var result = asrsch.GetListForm();
                    List<ARFormDisplay> forms = new List<ARFormDisplay>();
                    foreach (var r in result)
                    {
                        ARFormDisplay f = new ARFormDisplay(r);
                        forms.Add(f);
                    }
                    isSuccess = true;
                    GlobalCache.FormCache = forms;
                }
            }
            catch (Exception e)
            {
                isSuccess = false;
                exceptionMessage = e.Message;
            }

            if (GetFormsCompeleted != null)
            {
                Control control = GetFormsCompeleted.Target as Control;
                if (control != null)
                    control.Invoke(GetFormsCompeleted, new object[] { isSuccess, exceptionMessage });
                else
                    GetFormsCompeleted(isSuccess, exceptionMessage);
            }
        }
    }
}
